package com.android.place360.appsettings;

import java.util.Locale;
import com.android.place360.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;

import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.util.Log;




public class AppSetting extends PreferenceActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.setting);
		
		final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		final ListPreference listLanguage =(ListPreference) findPreference("language");
		final ListPreference number_of_results =(ListPreference) findPreference("number_of_results");
		final PreferenceCategory  catalogLanguage = (PreferenceCategory)findPreference("setting_catalog_language");
		final PreferenceCategory  catalogSearchOption = (PreferenceCategory)findPreference("setting_catalog_search_option");
		
		prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
			
			@Override
			public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
					String key) {
				Log.i("locale", "Locale: "+SharePreference_Setting.getLanguage(getApplicationContext()));
				Locale locale = new Locale(SharePreference_Setting.getLanguage(getApplicationContext()));
				AppSetting.ChangeLanguage(locale,getApplicationContext());
				 
				listLanguage.setTitle(R.string.setting_language_title);
				listLanguage.setSummary(R.string.setting_language_summary);
				listLanguage.setDialogTitle(R.string.setting_language_dialog);
				number_of_results.setTitle(R.string.setting_number_of_results_title);
				number_of_results.setSummary(R.string.setting_number_of_results_summary);
				number_of_results.setDialogTitle(R.string.setting_number_of_results_dialog);
				catalogLanguage.setTitle(R.string.setting_catalog_language);
				catalogSearchOption.setTitle(R.string.setting_catalog_search_option);
			}
		});
		
	}
	
	public static void ChangeLanguage(Locale locale, Context context){
		Resources res = context.getResources();
		Configuration newConfig = new Configuration(res.getConfiguration());
		newConfig.locale = locale;
		res.updateConfiguration(newConfig, null);
		
	}
}
